#include "classes/PlayerClass.hpp"
#include "classes/MapClass.hpp"
#include <algorithm>
PlayerClass::PlayerClass()
{
    mStats.SetAttribute("HP",20);
    mStats.SetNormal("HP", 20);
	mRightHand.Item.Name="None";
	mLeftHand.Item.Name="None";
	mBody.Item.Name="None";
	accessory.Item.Name="None";
}
int PlayerClass::GetAttribute(std::string theName)
{
    return mStats.GetAttribute(theName);
}
int PlayerClass::GetNormal(std::string theName)
{
    return mStats.GetNormal(theName);
}
void PlayerClass::Damage(int theDamage)
{
    mStats.SetAttribute("HP",mStats.GetAttribute("HP")-theDamage);
}

void PlayerClass::GiveItem(typeItem theItem,unsigned int theAmount)
{
    typeItemRef newItem;
    newItem.Item=theItem;
    newItem.Amount=theAmount;
	newItem.Inventory=&mInventory;
    int anIndex;
	if(newItem.Item.Stackable=="Yes" )
	{
		for(anIndex=0;anIndex<mInventory.size();anIndex++)
		{
			if(mInventory[anIndex].Item.Name==newItem.Item.Name)
			{
				mInventory[anIndex].Amount++;
				return;
			}
		}
    }
	if(mInventory.size()>PLAYER_ITEMS)
	{
		return;
	}
	newItem.Index=mInventory.size();
	mInventory.push_back(newItem);
}

void PlayerClass::DropItem(unsigned int theIndex)
{
    if(theIndex<0 || theIndex>mInventory.size())
        return;
    gMap->GetRoom(GetCoords().x,GetCoords().y,GetCoords().z)->Inventory.push_back(mInventory[theIndex]);
    mInventory.erase(mInventory.begin()+theIndex);
}
void PlayerClass::Equip(unsigned int theIndex)
{
	if(mInventory.at(theIndex).Item.Type=="Weapon")
	{
		if(mRightHand.Item.Name!="None")
		{
			GiveItem(mRightHand.Item,1);
		}
		mRightHand=mInventory.at(theIndex);
	}
	else if(mInventory.at(theIndex).Item.Type=="Armor")
	{
		if(mBody.Item.Name!="None")
		{
			GiveItem(mBody.Item,1);
		}
		mBody=mInventory.at(theIndex);
	}
	if(mInventory.at(theIndex).Item.Stackable=="No" || mInventory.at(theIndex).Amount==1)
	{
		mInventory.erase(mInventory.begin()+theIndex);
	}
	else
	{
		mInventory.at(theIndex).Amount--;
	}
}
typeInventory* PlayerClass::GetInventory()
{
    return &mInventory;
}